package com.example.homework3

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle

class FifthActivity : AppCompatActivity() {

   var seconds=0
    var running=false
    var wasRunning=false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_fifth)
        Log.d("life cycle","onCreate")

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        if(savedInstanceState!=null){
            seconds=savedInstanceState.getInt("seconds")
            running=savedInstanceState.getBoolean("running")
            wasRunning=savedInstanceState.getBoolean("wasRunning")
        }

        val START=findViewById<Button>(R.id.start_button)
        val STOP=findViewById<Button>(R.id.stop_button)
        val RESTART=findViewById<Button>(R.id.restart_button6)
        runTimer()
        START.setOnClickListener {
            running=true
        }
        STOP.setOnClickListener {
            running=false
        }
        RESTART.setOnClickListener {
            running=true
            seconds=0
        }

        val CLOSE=findViewById<Button>(R.id.close_button3)
        CLOSE.setOnClickListener {
            finish()
        }
    }


    fun runTimer(){
        val TIME=findViewById<TextView>(R.id.timer)
        val handler= Handler()
        val runnable=object:Runnable{
            @SuppressLint("DefaultLocale")
            override fun run(){
                val hours=seconds/3600
                val minutes=(seconds%3600)/60
                val secs=seconds%60
                TIME.text=String.format("%02d:%02d:%02d",hours,minutes,secs)
                if(running){
                    seconds++
                }
                handler.postDelayed(this,1000)
            }
        }
        handler.post (runnable)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt("seconds",seconds)
        outState.putBoolean("running",running)
        outState.putBoolean("wasRunning",wasRunning)
        Log.d("life cycle","onSaveInstanceState")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("life cycle","onDestroy")
    }

    override fun onStart() {
        super.onStart()
        Log.d("life cycle","onStart")
    }

    override fun onStop() {
        super.onStop()
        Log.d("life cycle","onStop")
    }

    override fun onResume() {
        super.onResume()
        Log.d("life cycle","onResume")
        if(wasRunning){
            running=true
        }
    }

    override fun onPause() {
        super.onPause()
        Log.d("life cycle","onPause")
        wasRunning=running
        running=false
    }
}